package com.wu.controller;

import com.wu.feign.MenuFeign;
import com.wu.pojo.Menu;
import com.wu.pojo.Type;
import com.wu.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuFeign menuFeign;
//这个是我们测试用的，laYUI会默认给我们传递index和limit所以我们不需要用这个了，改为下面的
//    @GetMapping("/findall/{index}/{limit}")
//    public List<Menu> findAll(@PathVariable("index") int index, @PathVariable("limit") int limit){
//        return menuFeign.findAll(index, limit);
//    }
    @GetMapping("/findall")
    @ResponseBody//返回数据，不返回视图
    public MenuVo findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        int index=(page-1)*limit;
        return menuFeign.findAll(index, limit);
    }

    @GetMapping("/redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }

    //2.删除操作
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        menuFeign.deleteById(id);
        return "redirect:/menu/redirect/index";
    }

    //3.查询菜品所属系列操作
    @GetMapping("/findTypes")
    public ModelAndView findTypes(){
        ModelAndView modelAndView = new ModelAndView();
        List<Type> typeList = menuFeign.findTypes();
        modelAndView.setViewName("menu_add");
        modelAndView.addObject("list",typeList);
        return modelAndView;
    }

    //4.保存方法
    @PostMapping("/save")
    public String save(Menu menu){
        menuFeign.save(menu);
        return "redirect:/menu/redirect/index";
    }

    //5.查询一个
    @GetMapping("/findById/{id}")
    public Menu findById(@PathVariable("id")long id){
        return menuFeign.findById(id);
    }

    //6.更新方法
    @PutMapping("/update")
    public void update(Menu menu){
        menuFeign.update(menu);
    }

}
